///Users/shiyazhou/Desktop/Classes
//  MainTabBar.swift
//  WeiBo
//
//  Created by 史亚洲 on 16/1/10.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit


class MainTabBar: UITabBar {
    //MainTabBar既支持代码创建,又支持xib/sb创建
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        //添加加号按钮
        addSubview(plusButton)
        print(plusButton)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
          }
    
    
    //在这个方法中设置所有的子视图的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //修改所有的UITabBarbutton的frame,并且把中间位置让出来
        let w = self.bounds.width / 5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        //遍历所有的子视图,修改大小,定义索引系数
        var index: CGFloat = 0
        for subview in subviews {
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                subview.frame = CGRectOffset(rect, w * index, 0)
                index += (index == 1 ? 2 : 1)
                
            }
            
        }
        
        //修改加号按钮的位置
        plusButton.frame = CGRectOffset(rect, w * 2, 0)
    
}
    
    //懒加载获取加号按钮的位置
lazy var plusButton: UIButton = {
    
    //1.获取自定义类型按钮
    let btn = UIButton()
    
    //2.设置按钮图片
    btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
    btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
    
    //3.设置按钮背景图片
    btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
    btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
    
    //4.自适应按钮大小,懒加载本质就是一个闭包,需要加self
    btn.sizeToFit()
    return btn
    
    
    
}()



}
